package com.chinacoal.hr.levelimpl.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.entity.TransPaperDetailList;
import com.chinacoal.hr.levelimpl.dao.TransPaperDetailListDao;
import com.chinacoal.hr.levelimpl.service.TransPaperDetailListService;
import com.chinacoal.hr.levelimpl.utils.DateUtil;
import com.chinacoal.hr.levelimpl.utils.StaticDicts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: YunTao.Li
 * @create: 2019-11-17 13:51
 * @description:
 **/
@Service
public class TransPaperDetailListServiceImpl implements TransPaperDetailListService {

    @Autowired
    private TransPaperDetailListDao transPaperDetailListDao;

    /**
     * 获得员工事务单明细记录(他叫汇总表实际上是明细记录）
     *
     * @param beginDate : yyyy-MM-dd
     * @param endDate   :yyyy-MM-dd
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.TransPaperDetailList>
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    @Override
    public List<TransPaperDetailList> getTransPaperDetailList(String beginDate, String endDate) throws Throwable {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("beginDate", beginDate);
        condition.put("endDate", endDate);

        List<TransPaperDetailList> transPaperDetailLists = this.transPaperDetailListDao.getTransPaperDetailList(condition);
        return transPaperDetailLists;
    }

    /**
     * 待汇总的事务单记录
     *
     * @param beginDate :
     * @param endDate   :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.TransPaperDetailList>
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    public List<TransPaperDetailList> getPreValidTransPaperDetailList(String beginDate, String endDate) throws Throwable {
        List<TransPaperDetailList> transPaperDetailLists = this.getPreValidTransPaperDetailList(null, beginDate, endDate);
        return transPaperDetailLists;
    }

    /**
     * 待汇总的事务单记录
     *
     * @param beginDate :
     * @param endDate   :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.TransPaperDetailList>
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    public List<TransPaperDetailList> getPreValidTransPaperDetailList(String empCode, String beginDate, String endDate) throws Throwable {
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("empCode", empCode);
        condition.put("beginDate", beginDate);
        condition.put("endDate", endDate);
        condition.put("bizStatus", StaticDicts.BUSINESS_STATUS_APPROVALED);
//        condition.put("effectFlag", StaticDicts.EFFECTFLAG_INVALID);

        List<TransPaperDetailList> transPaperDetailLists = this.transPaperDetailListDao.getTransPaperDetailList(condition);
        return transPaperDetailLists;
    }

    @Override
    public IPage<TransPaperDetailList> getTransPaperDetailListList(Map<String, Object> condition) throws Throwable {
        return null;
    }

    @Override
    public Map<String, String> insertTransPaperDetailList(TransPaperDetailList record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");

        try {
            transPaperDetailListDao.insertTransPaperDetailList(record);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }

    @Override
    public Map<String, String> updateTransPaperDetailList(TransPaperDetailList record) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");

        try {
            transPaperDetailListDao.updateTransPaperDetailList(record);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }

    /**
     * 按模板修改员工事务单汇总表
     *
     * @param template :
     * @param value    :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2019/11/27 2019/11/27
     */
    @Override
    public Map<String, String> updateTransPaperDetailListByTemplate(TransPaperDetailList template, TransPaperDetailList value) throws Throwable {
        Map<String, String> result = new HashMap<String, String>();
        result.put("code", "-1");

        try {
            transPaperDetailListDao.updateTransPaperDetailListByTemplate(template, value);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        result.put("code", "1");
        return result;
    }

    @Override
    public Map<String, String> deleteTransPaperDetailList(String paListId) throws Throwable {
        return null;
    }

    /**
     * 提交事务单，插入员工事务单汇总表</br>
     * XIP_HR_PA_LIST
     *
     * @param paDocId       : 事务单id
     * @param docTypeId     : 事务单类型id
     * @param paType        : 事假、病假、出差、婚嫁
     * @param startDate     : 开始日期
     * @param startTimeType : 开始时间类型：1上午、2下午、3全天
     * @param endDate       : 结束日期
     * @param endTimeType   : 结束日期类型：1上午、2下午、3全天
     * @return : void
     * @author : YunTao.Li
     * @date : 2019/11/19 2019/11/19
     */
    public void submitTransPaperDetailList(String paDocId,
                                           String docTypeId,
                                           String paType,
                                           String empCode,
                                           Date startDate,
                                           String startTimeType,
                                           Date endDate,
                                           String endTimeType,
                                           String holidayType) throws Throwable {
        // step1 : 获取自然日
        int ndays = DateUtil.getNatureDayMargin(startDate, endDate);

        // step2 : 循环自然日
        for (int i = 1; i <= ndays; i++) {
            String timeType = StaticDicts.TIME_TYPE_ALLDAY; //默认为全天
            // step2.1 : 从开始日期计算,currentPaDate为startDate + i - 1 天
            Calendar startDateCalendar = Calendar.getInstance();
            startDateCalendar.setTime(startDate);
            startDateCalendar.set(Calendar.DATE, startDateCalendar.get(Calendar.DATE) + (i - 1));
            Date currentPaDate = startDateCalendar.getTime();

            // step2.2 : 判断timeType,如果第一天则timeType = startTimeType
            // step2.3 : 判断timeType,如果最后一天则timeType = endTimeType
            if (i == 1) { //第一天
                timeType = startTimeType;
            } else if (i == ndays) { // 最后一天
                timeType = endTimeType;
            }

            TransPaperDetailList paperDetailList = new TransPaperDetailList();  //员工事务单汇总表(xip_hr_pa_list)实例
            paperDetailList.setPaDocId(paDocId);
            paperDetailList.setDocTypeId(docTypeId);
            paperDetailList.setPaType(paType);
            paperDetailList.setEmpCode(empCode);
            paperDetailList.setPaDate(currentPaDate);
            paperDetailList.setTimeType(timeType);
            paperDetailList.setHolidayType(holidayType);
            paperDetailList.setBusinessStatus(StaticDicts.BUSINESS_STATUS_SUBMITTED); // 新增的提交一定是待审批状态
            this.insertTransPaperDetailList(paperDetailList);
        }
    }

    /**
     * 根据事务单id来删除,员工事务单汇总表
     *
     * @param paDocId :
     * @return : java.util.Map<java.lang.String,java.lang.String>
     * @author : YunTao.Li
     * @date : 2019/12/4 2019/12/4
     */
    @Override
    public void deleteTransPaperDetailListByDocId(String paDocId) throws Throwable {
        Map<String, Object> condition = new HashMap<>();
        condition.put("paDocId", paDocId);

        this.transPaperDetailListDao.deleteTransPaperDetailListByCondition(condition);
    }
}
